package com.lmx.log;

import java.util.LinkedList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.repoDomain.tmodel.ReqLog;

/**
 * @author huibati 2017年3月24日
 */
public class LogQueue {
	private Log logger = LogFactory.getLog(this.getClass());
	// 存储元素的LinkedList
	private LinkedList<ReqLog> logList = new LinkedList<ReqLog>();// 日志队列

	public void addReqLog(ReqLog data) {
		logger.debug("log ip " + data.getClientIp());
		logList.addFirst(data);

	}

	public ReqLog popReqLog() {
		// 如果队列为空，等待，等待其他线程添加元素
		if (logList.isEmpty()) {
			for (int i = 0; i < 3; i++) {
				if (logList.isEmpty()) {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						logger.debug(" popReqLog error " + e);
					}
				} else {
					//logger.debug("popReqLog " + logList.getLast().getClientIp());
					return logList.pollLast();
				}
			}
			return null;
		} else {
			//logger.debug("popReqLog " + logList.getLast().getClientIp());
			return logList.pollLast();
		}
	}

}
